package com.ruoyi.order.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.basic.service.impl.BaseServiceImpl;
import com.param.concat.IToExamineParamBySearch;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.order.mapper.OrderLogMapper;
import com.ruoyi.order.domain.OrderLog;
import com.ruoyi.order.service.IOrderLogService;

/**
 * 订单日志Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-01-09
 */
@Service
public class OrderLogServiceImpl extends BaseServiceImpl<Long, OrderLog> implements IOrderLogService 
{
	private static int[] ORDER_STATUS = new int[]{
			21,
			7
	};
	
	// 1 反馈 2 售后
	// 23 变更服务类型:反馈 
	// 24 变更服务类型:售后
	
	private static int[] CHANGE_STATUS = new int[]{
			-1,
			23,
			24
	};
	
	
    public OrderLogServiceImpl(OrderLogMapper baseMapper) {
		super(baseMapper);
		// TODO Auto-generated constructor stub
	}


    /**
     * 查询订单日志
     * 
     * @param id 订单日志主键
     * @return 订单日志
     */
    @Override
    public OrderLog selectById(Integer id)
    {
        return baseMapper.selectById(id);
    }

    /**
     * 查询订单日志列表
     * 
     * @param orderLog 订单日志
     * @return 订单日志
     */
    @Override
    public List<OrderLog> selectList(OrderLog orderLog)
    {
        return baseMapper.selectList(orderLog);
    }

    /**
     * 新增订单日志
     * 
     * @param orderLog 订单日志
     * @return 结果
     */
    @Override
    public int insert(OrderLog orderLog)
    {
        return baseMapper.insert(orderLog);
    }

    /**
     * 修改订单日志
     * 
     * @param orderLog 订单日志
     * @return 结果
     */
    @Override
    public int update(OrderLog orderLog)
    {
        return baseMapper.update(orderLog);
    }

    /**
     * 批量删除订单日志
     * 
     * @param ids 需要删除的订单日志主键
     * @return 结果
     */
    @Override
    public int deleteByIds(Integer[] ids)
    {
        return baseMapper.deleteByIds(ids);
    }

    /**
     * 删除订单日志信息
     * 
     * @param id 订单日志主键
     * @return 结果
     */
    @Override
    public int deleteById(Integer id)
    {
        return baseMapper.deleteById(id);
    }


	@Override
	@Transactional(propagation = Propagation.MANDATORY)
	public int insertLog(int orderId, 
			int userId, int status, int payManagerId, int type, String requirement) {
		
		OrderLog orderLog = generate(orderId, userId, status, payManagerId, type);
		
		orderLog.setRequirement(requirement);
		
		return baseMapper.insert(orderLog);
	}

	
	@Override
	@Transactional(propagation = Propagation.MANDATORY)
	public int insertLog(int orderId, 
			int userId, int status, int payManagerId, int type) {
		
		OrderLog orderLog = generate(orderId, userId, status, payManagerId, type);
		
		
		return baseMapper.insert(orderLog);
	}

	

	private OrderLog generate(int orderId, int userId, int status, int payManagerId, int type) {
		OrderLog orderLog = new OrderLog();
		
		orderLog.setType(type);
		
		orderLog.setOrderId(orderId);
		
		orderLog.setPayManagerId(payManagerId);
		
		orderLog.setUserId(userId);
		
		orderLog.setOrderStatus(status);
		
		insertProperties(orderLog);
		return orderLog;
	}

	/**
	 * 已报价
	 */
	@Override
	@Transactional(propagation = Propagation.MANDATORY)
	public int quotationLog(int orderId, int userId, int status, int payManagerId, BigDecimal price) {
		
		OrderLog orderLog = this.generate(orderId, userId, status, payManagerId, 2);
		
		orderLog.setPrice(price);
		
		return baseMapper.insert(orderLog);
	}


	@Override
	protected Class<OrderLog> getEntityClass() {
		// TODO Auto-generated method stub
		return OrderLog.class;
	}


	@Override
	protected OrderLog[] getEntityObject(int size) {
		// TODO Auto-generated method stub
		return new OrderLog[size];
	}


	@Override
	@Transactional(propagation = Propagation.MANDATORY)
	public int toExamination(IToExamineParamBySearch param) {
		OrderLog orderLog = this.generate(param);
		int insert = this.insert(orderLog);
		
		param.setOrderLogId(orderLog.getId());
		
		return insert;
	}
	
	/**
	 * 
	 * @param param
	 * @return
	 */
	private OrderLog generate(IToExamineParamBySearch param) {
		
		OrderLog orderLog = new OrderLog();
		
		orderLog.setOrderId(param.getId());
		
		orderLog.setPayManagerId(param.getPayManagerId());
		
		orderLog.setUserId(param.getUserId());
		
		orderLog.setOrderStatus(ORDER_STATUS[param.getStatus()]);
		
		orderLog.setRequirement(param.getRequirement());
		
		orderLog.setCreateTime(System.currentTimeMillis() / 1000);
		
		return orderLog;
	}


	@Override
	public int changeServiceType(IToExamineParamBySearch param) {
		
		OrderLog orderLog = new OrderLog();
		
		orderLog.setOrderId(param.getId());
		
		orderLog.setPayManagerId(param.getPayManagerId());
		
		orderLog.setUserId(param.getUserId());
		
		orderLog.setOrderStatus(CHANGE_STATUS[param.getServiceType()]);
		
		orderLog.setType(2);
		
		orderLog.setRequirement("");
		
		orderLog.setCreateTime(System.currentTimeMillis() / 1000);
		
		return this.insert(orderLog);
	}
}